package com.example.maili.woxiamg.activity;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.fragment.ActivityFragment;
import com.example.maili.woxiamg.fragment.HomeFragment;
import com.example.maili.woxiamg.fragment.OrderFragment;
import com.example.maili.woxiamg.fragment.PersonalFragment;
import com.example.maili.woxiamg.utils.AppUtils;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout main_container;
    private TextView tv_home, tv_news, tv_order, tv_me;//底部导航
    private LinearLayout rl_bottom;
    private ImageView img_make;
    private HomeFragment homeFragment;

    private int currentId = R.id.tv_home;// 当前选中id,默认是主页
    private OrderFragment orderFragment;
    private ActivityFragment activityFragment;
    private PersonalFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();//隐藏掉整个ActionBar，包括下面的Tabs
        main_container = (FrameLayout) findViewById(R.id.main_container);
        tv_home = (TextView) findViewById(R.id.tv_home);
        tv_news = (TextView) findViewById(R.id.tv_news);
        tv_order = (TextView) findViewById(R.id.tv_order);
        tv_me = (TextView) findViewById(R.id.tv_me);
        rl_bottom = (LinearLayout) findViewById(R.id.rl_bottom);
        img_make = (ImageView) findViewById(R.id.iv_make);
        tv_home.setOnClickListener(this);
        tv_news.setOnClickListener(this);
        tv_order.setOnClickListener(this);
        tv_me.setOnClickListener(this);
        rl_bottom.setOnClickListener(this);
        img_make.setOnClickListener(this);

        tv_home.setSelected(true);//默认选中首页

        /**
         * 默认加载首页
         */
        homeFragment = new HomeFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.main_container, homeFragment).commit();

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_make) {
            AppUtils.toast(this, "发布");
            return;
        }
        if (view.getId() != currentId) {//如果当前选中跟上次选中的一样,不需要处理
            changeSelect(view.getId());//改变图标跟文字颜色的选中
            changeFragment(view.getId());//fragment的切换
            currentId = view.getId();//设置选中id
        }
    }

    /**
     * 改变fragment的显示
     *
     * @param resId
     */
    private void changeFragment(int resId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个Fragment事务

        hideFragments(transaction);//隐藏所有fragment
        if (resId == R.id.tv_home) {//主页
            if (homeFragment == null) {//如果为空先添加进来.不为空直接显示
                homeFragment = new HomeFragment();
                transaction.add(R.id.main_container, homeFragment);
            } else {
                transaction.show(homeFragment);
            }
        } else if (resId == R.id.tv_order) {//订单
            if (orderFragment == null) {
                orderFragment = new OrderFragment();
                transaction.add(R.id.main_container, orderFragment);
            } else {
                transaction.show(orderFragment);
            }
        } else if (resId == R.id.tv_news) {//消息
            if (activityFragment == null) {
                activityFragment = new ActivityFragment();
                transaction.add(R.id.main_container, activityFragment);
            } else {
                transaction.show(activityFragment);
            }
        } else if (resId == R.id.tv_me) {//我
            if (meFragment == null) {
                meFragment = new PersonalFragment();
                transaction.add(R.id.main_container, meFragment);
            } else {
                transaction.show(meFragment);
            }
        }
        transaction.commit();//一定要记得提交事务
    }


    /**
     * 改变TextView选中颜色
     *
     * @param resId
     */
    private void changeSelect(int resId) {
        tv_home.setSelected(false);
        tv_news.setSelected(false);
        tv_order.setSelected(false);
        tv_me.setSelected(false);
        switch (resId) {
            case R.id.tv_home:
                tv_home.setSelected(true);
                break;
            case R.id.tv_news:
                tv_news.setSelected(true);
                break;
            case R.id.tv_order:
                tv_order.setSelected(true);
                break;
            case R.id.tv_me:
                tv_me.setSelected(true);
                break;
        }
    }

    /**
     * 显示之前隐藏所有fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null)//不为空才隐藏,如果不判断第一次会有空指针异常
            transaction.hide(homeFragment);
        if (orderFragment != null)
            transaction.hide(orderFragment);
        if (activityFragment != null)
            transaction.hide(activityFragment);
        if (meFragment != null)
            transaction.hide(meFragment);
    }
}
